package com.converter.huawei.api;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.converter.huawei.utils.FfmpegUtils;
import com.converter.huawei.utils.HuaweiUtils;
import com.converter.huawei.utils.RtspToMP4Util;
import com.converter.huawei.vo.back.BackUrlResultVO;
import com.converter.huawei.vo.LoginVO;
import com.converter.huawei.vo.pictrue.*;
import com.converter.huawei.vo.priview.PriviewUrlResultVO;
import com.converter.huawei.vo.priview.RtspVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 崔晓阳.
 * Email: 15373889135@189.cn
 * Date: 2022/06/22
 * Time: 20:46
 * Project: huawei
 */
@RestController
@RequestMapping("/live")
public class HWController {


    @GetMapping("/demo")
    public String demo() {
        return "Hello battcn";
    }
    @GetMapping("/demo1")
    public String login() {
        LoginVO vo = HuaweiUtils.webLogin("192.168.10.251","admin","HuaWei@123");
//        LoginVO vo = HuaweiUtils.webLogin("192.168.1.50","admin","Admin12345");
        return JSONUtil.toJsonStr(vo);
    }

    @GetMapping("/cameraControl/{ip}/{channel}/{code}")
    public String cameraControl(@PathVariable("ip") String ip,@PathVariable("channel") String channel,@PathVariable("code") String code) {
        String userName = "admin";
        String pwd = "Admin12345";
        HuaweiUtils.webLogin(ip, userName, pwd);
        String res = HuaweiUtils.cameraControl(channel,code);
        BackUrlResultVO vo = JSONUtil.toBean(res, BackUrlResultVO.class);
        return JSONUtil.toJsonStr(vo.getResult());
    }


    /**
     * 获取直播流
     * @param ip
     * @return
     */
/*    @GetMapping("/getRtspUrl")
    public String GetRtspUrl(String ip,String channel,String userName,String pwd) {
//        登录 //admin HuaWei@123
         HuaweiUtils.webLogin(ip,userName,pwd);

        String res = HuaweiUtils.GetRtspUrl(ip,channel);
        PriviewUrlResultVO vo = JSONUtil.toBean(res, PriviewUrlResultVO.class);
        String rtsp = vo.getData().getChannel_info().get(0).getSubstream();
        return rtsp;
    }*/
    /**
     * 获取直播流
     * @param ip
     * @return
     */
    @GetMapping("/getRtspUrl")
    public Result GetRtspUrl(String ip,String[] channel,String userName,String pwd) {
//        登录 //admin HuaWei@123
        HuaweiUtils.webLogin(ip,userName,pwd);

        String res = HuaweiUtils.GetRtspUrl(ip,channel,userName);
//        JSONObject resObject = JSONUtil.parseObj(res);
//        JSONArray channels = resObject.getJSONArray("data");
//        List<PriviewUrlResultVO> priviewUrlResultVOS = JSONUtil.toList(channels, PriviewUrlResultVO.class);

        PriviewUrlResultVO vo = JSONUtil.toBean(res, PriviewUrlResultVO.class);
        List<RtspVO> channel_info = vo.getData().getChannel_info();
        List<String> substreamList = channel_info.stream().map(c -> c.getSubstream()).collect(Collectors.toList());
        return new Result().ok(substreamList);
    }


    /**
     * 获取录播流
     * @param ip
     * @return
     */
    @GetMapping("/playbacRtspkUrl")
        public String getPlaybacRtspkUrl(String ip,String channel,String userName,String pwd,String startTime,String endTime) {
//        登录
        HuaweiUtils.webLogin(ip,userName,pwd);
        String res = HuaweiUtils.getPlaybacRtspkUrl(ip,channel,startTime,endTime,userName);
        BackUrlResultVO vo = JSONUtil.toBean(res, BackUrlResultVO.class);
        return vo.getData().getPlayback_url();
    }

    @Autowired
    private RtspToMP4Util rtspToMP4Util;

    /**
     * 导出录播流 mp4
     * @param ip
     * @return
     */
    @GetMapping("/getPlaybacRtspToMP4")
    public String getPlaybacRtspToMP4(String ip,String channel,String userName,String pwd,String startTime,String endTime) {
//        登录
        HuaweiUtils.webLogin(ip,userName,pwd);

        String res = HuaweiUtils.getDownloadRtspkUrl(startTime, endTime);

        BackUrlResultVO vo = JSONUtil.toBean(res, BackUrlResultVO.class);
        String download_url = vo.getData().getDownload_url();
        String ffmpegPath="D:\\1work\\FFmpeg\\ffmpeg-20180227-fa0c9d6-win64-static\\bin\\ffmpeg.exe";
        String streamUrl = download_url;
        String FilePath="D:\\aaaa.mp4";
        Process process = rtspToMP4Util.StartRecord(ffmpegPath, streamUrl, FilePath);
////        boolean b = rtspToMP4Util.stopRecord(process);
        return "";
    }

    /**
     * 导出录播流 mp4
     * @param ip
     * @return
     */
    @GetMapping("/getPlaybacRtspToMP42")
    public String getPlaybacRtspToMP42(String ip,String channel,String userName,String pwd,String startTime,String endTime) {
//        登录
        HuaweiUtils.webLogin(ip,userName,pwd);

        String res = HuaweiUtils.getDownloadRtspkUrl(startTime, endTime);

        BackUrlResultVO vo = JSONUtil.toBean(res, BackUrlResultVO.class);
        String download_url = vo.getData().getDownload_url();
        String ffmpegPath="D:\\1work\\FFmpeg\\ffmpeg-20180227-fa0c9d6-win64-static\\bin\\ffmpeg.exe";
        String streamUrl = download_url;
        String FilePath="D:\\aaaa.mp4";
        ArrayList<String> strings = new ArrayList<>();
        strings.add("D:\\1111.mp4");
        strings.add("D:\\2222.mp4");
//        FfmpegUtils.mergeVideos(strings);

//        List<String> command = new ArrayList<>();
//        command.add(ffmpegPath);
//        command.add("-rtsp_transport");
//        command.add("tcp");
//        command.add("-i");
//        command.add(streamUrl);
//        command.add("-rtsp_transport");
//        command.add("tcp");
//        command.add("-i");
//        command.add(streamUrl);
//        command.add("-filter_complex");
//        command.add("\"[0:v]pad=iw*2:ih*2[a];[a][1:v]overlay=w[b];[b][2:v]overlay=0:h[c];[c][3:v]overlay=w:h\"");
//        command.add("ttt.mp4");
//        FfmpegUtils.commandStart(command);

////        boolean b = rtspToMP4Util.stopRecord(process);
        return download_url;
    }



//    @GetMapping("/close")
//    public String close(){
//        boolean b = rtspToMP4Util.stopRecord(process);
//        return
//    }

    /**
     * 获取抓拍的图片列表
     * @param ip
     * @param pageNum
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/SnapedFaces")
    public Result<PicPage> SnapedFaces(String ip, String channel,String userName,String pwd,Integer pageNum, String startTime, String endTime) {
        //        登录
        HuaweiUtils.webLogin(ip,userName,pwd);
        Integer startIndex = (pageNum-1)*30;
        PicPage page = HuaweiUtils.SnapedFaces(ip,channel,startIndex,startTime,endTime);
        Result<PicPage> res = new Result<>();
        return res.ok(page);
    }
//    @GetMapping("/videoAlarm")
//    public String videoAlarm() {
//        String res = HuaweiUtils.videoAlarm("192.168.10.211");
//        return res;
//    }
//
//    @GetMapping("/picture")
//    public String pictrue() {
//        String res = HuaweiUtils.picture("192.168.10.211");
//        return res;
//    }
//    @GetMapping("/showPic")
//    public String showPic(@RequestParam String picId,String ip) {
//        String res = HuaweiUtils.showPic(picId,ip);
//        PictureDetailVO vo = JSONUtil.toBean(res, PictureDetailVO.class);
//        return vo.getData().getPicture().getImage();
//    }
//
//    @GetMapping("/getToken")
//    public String getToken() {
//        String res = HuaweiUtils.getToken();
//        return JSONUtil.toJsonStr(res);
//    }
    @GetMapping("/demo2")
    public String find() {
        String vo = HuaweiUtils.range();
        return JSONUtil.toJsonStr(vo);
    }
}
